import hashlib
import os

from sqlalchemy.engine import URL

from common.exchanges.contants import Exchange
from common.helper import global_config_parser


os.environ.setdefault('TZ', 'Asia/Shanghai')

CONFIG_PARSER = global_config_parser()
PORT = CONFIG_PARSER.get('common', 'port')


SQLALCHEMY_DATABASE_URI = URL.create(
    drivername='mysql+pymysql',
    username=CONFIG_PARSER.get("db", "username"),
    password=CONFIG_PARSER.get("db", "password"),
    host=CONFIG_PARSER.get("db", "host"),
    port=CONFIG_PARSER.get("db", "port") and CONFIG_PARSER.get('db', 'port').isdigit() and int(CONFIG_PARSER.get('db', 'port')) or 3306,
    database=CONFIG_PARSER.get("db", "db")
)

SUPPORT_BASE_CURRENCIES = CONFIG_PARSER.get('common', 'base_currencies', ).split(',')
SUPPORT_MARGIN_CURRENCIES = CONFIG_PARSER.get('common', 'margin_currencies', ).split(',')
EXCHANGE_ENV = CONFIG_PARSER.get('common', 'exchange_env', fallback='test')

# common
SECRET = CONFIG_PARSER.get('common', 'secret')

# AUTH
AUTH_SECRET = hashlib.md5(CONFIG_PARSER.get('auth', 'auth_token_secret').encode()).hexdigest()
try:
    ALLOW_USERS = [i for i in CONFIG_PARSER.get('auth', 'auth_allow_users').split(',') if i.strip()]
except (ValueError, KeyError, TypeError):
    ALLOW_USERS = []


# Trade
SUPPORT_EXCHANGES = [Exchange.BINANCE, Exchange.DERIBIT]

RISK_MM_RATIO = 0.5
